package com.example.asynctaskdemo.okhttp;

import android.text.TextUtils;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by xubp on Date: 2022/11/3
 */
public class OjjlgRequestUtil {
    private static final String TAG = "OjjlgRequestUtil";

    /**
     * get请求，将键值对凭接到url上
     */
    public static String clwuyGetUrl(String path, Map<String, String> paramsMap) {
        if(paramsMap != null){
            path = path+"?";
            for (String key: paramsMap.keySet()){
                path = path + key+"="+paramsMap.get(key)+"&";
            }
            path = path.substring(0,path.length()-1);
        }
        Log.e(TAG, "clwuypath--->"+path );
        return path;
    }

    /**
     * 根据键值对参数得到body
     */
    public static String clwuyGetPostBodyFormParameMap(Map<String, String> params) {//throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        try {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (first) {
                    first = false;
                }else {
                    result.append("&");
                }
                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                result.append(TextUtils.isEmpty(entry.getValue())?"":URLEncoder.encode(entry.getValue(), "UTF-8"));
            }
            return result.toString();
        } catch (UnsupportedEncodingException e) {
            return "";
        }

    }
}
